﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;
    using WMS6.Web.UserControls;

    public class PickDetailList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReport;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F7052;
        protected GridView gvPickDetail;
        protected HtmlInputHidden hdnAction;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnSelectIndex;
        protected HtmlInputHidden hdnTimeValue;
        protected HtmlInputButton htmlbtnAction;
        protected Label lblMsg;
        protected Label lblPickDetail;
        protected Label lblTotal;
        protected Label lblTotalQty;
        protected Label lblTotalQytText;
        protected LinkButton lnkbtnActualShipDate;
        protected LinkButton lnkbtnCancelShipping;
        protected LinkButton lnkbtnClose;
        protected LinkButton lnkbtnConfirmPicked;
        protected LinkButton lnkbtnRevertPicked;
        protected LinkButton lnkbtnRevertPickedAllocated;
        protected LinkButton lnkbtnShipping;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnActualShipDate;
        protected HtmlTableCell tdlnkbtnCancelShipping;
        protected HtmlTableCell tdlnkbtnClose;
        protected HtmlTableCell tdlnkbtnConfirmPicked;
        protected HtmlTableCell tdlnkbtnRevertPicked;
        protected HtmlTableCell tdlnkbtnRevertPickedAllocated;
        protected HtmlTableCell tdlnkbtnShipping;
        private decimal totalqty;
        protected HtmlInputText txtActualShipDate;

        private void BindData2PickDetailList(PickDetailQueryEntity pickDetailQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(pickDetailQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = OutboundServiceClientProxy.GetInstance().GetPickDetailInfoByQueryEntity(pickDetailQuery);
            base.BindData2GridView(this.gvPickDetail, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnActualShipDate_Click(object sender, EventArgs e)
        {
            Exception ex;
            List<string> strStatus = base.GetSelectedIDsInGridView(this.gvPickDetail, "STATUS");
            List<string> PICK_DETAIL_ID = base.GetSelectedIDsInGridView(this.gvPickDetail, "PICK_DETAIL_ID");
            List<string> ORDER_ID = base.GetSelectedIDsInGridView(this.gvPickDetail, "ORDER_ID");
            List<string> LINE_ID = base.GetSelectedIDsInGridView(this.gvPickDetail, "ORDER_LINE_ID");
            string strTemp = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(this.hdnTimeValue.Value))
                {
                    this.lblMsg.Text = "请输入修改时间！";
                }
                else
                {
                    try
                    {
                        strTemp = OutboundServiceClientProxy.GetInstance().UpateActualShipDateByPickDetailID(PICK_DETAIL_ID, ORDER_ID, LINE_ID, this.hdnTimeValue.Value);
                        if (!string.IsNullOrEmpty(strTemp))
                        {
                            this.lblMsg.Text = strTemp + "更新成功!";
                        }
                    }
                    catch (Exception exception1)
                    {
                        ex = exception1;
                        this.lblMsg.Text = ex.Message;
                    }
                    finally
                    {
                        ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type='text/javascript'> ActualShipClose();</script>", false);
                    }
                }
            }
            catch (Exception exception2)
            {
                ex = exception2;
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            PickDetailQueryEntity pickDetailQuery = this.GetPickDetailQueryEntity();
            pickDetailQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = pickDetailQuery.CurrentPage;
            pickDetailQuery.CurrentPage = "0";
            this.gvPickDetail.AllowSorting = false;
            this.BindData2PickDetailList(pickDetailQuery);
            this.hdnActionID.Value = "";
            this.gvPickDetail.ShowFooter = false;
            pickDetailQuery.IsGetAll = false;
            pickDetailQuery.CurrentPage = currentPageIndex;
            this.SetPickDetailQueryEntity(pickDetailQuery);
            base.Export2Excel("PickDetailQuery.xls", this.gvPickDetail);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PickDetailQueryEntity pickDetailQuery = this.GetPickDetailQueryEntity();
            pickDetailQuery.CurrentPage = "0";
            this.BindData2PickDetailList(pickDetailQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PickDetailQueryEntity pickDetailQuery = this.GetPickDetailQueryEntity();
            pickDetailQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2PickDetailList(pickDetailQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PickDetailQueryEntity pickDetailQuery = this.GetPickDetailQueryEntity();
            if (int.Parse(pickDetailQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                pickDetailQuery.CurrentPage = (int.Parse(pickDetailQuery.CurrentPage) + 1).ToString();
                this.BindData2PickDetailList(pickDetailQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            PickDetailQueryEntity pickDetailQuery = this.GetPickDetailQueryEntity();
            if (int.Parse(pickDetailQuery.CurrentPage) > 0)
            {
                pickDetailQuery.CurrentPage = (int.Parse(pickDetailQuery.CurrentPage) - 1).ToString();
                this.BindData2PickDetailList(pickDetailQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            this.LoadData();
        }

        private void ConfirmPicked()
        {
            try
            {
                int i;
                List<string> strStatus = base.GetSelectedIDsInGridView(this.gvPickDetail, "STATUS");
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPickDetail, "PICK_DETAIL_ID");
                List<string> ToLoc = new List<string>();
                for (i = 0; this.gvPickDetail.Rows.Count > i; i++)
                {
                    if (((CheckBox) this.gvPickDetail.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        if (!string.IsNullOrEmpty(((TextBox) this.gvPickDetail.Rows[i].FindControl("txtToLoc")).Text))
                        {
                            ToLoc.Add(((TextBox) this.gvPickDetail.Rows[i].FindControl("txtToLoc")).Text);
                        }
                        else
                        {
                            this.lblMsg.Text = "拣货单号: " + this.gvPickDetail.DataKeys[i]["PICK_DETAIL_ID"].ToString() + "请输入目标货位!";
                            return;
                        }
                    }
                }
                if (strStatus.Count > 0)
                {
                    for (i = 0; strStatus.Count > i; i++)
                    {
                        if ((strStatus[i].ToString().ToUpper() != "NEW") && (strStatus[i].ToString().ToUpper() != "RELEASED"))
                        {
                            this.lblMsg.Text = selectedID[i] + base.GetGlobalMsgResource("F7500_005");
                            return;
                        }
                    }
                    if (((selectedID != null) && (selectedID.Count > 0)) && !string.IsNullOrEmpty(OutboundServiceClientProxy.GetInstance().ConfirmPicked(selectedID, ToLoc, base.UserInfo.UserID)))
                    {
                        string temp = string.Empty;
                        for (i = 0; i < selectedID.Count; i++)
                        {
                            temp = temp + selectedID[i] + ",";
                        }
                        this.lblMsg.Text = string.IsNullOrEmpty(temp) ? "" : (temp.Substring(0, temp.Length - 1) + base.GetGlobalMsgResource("F7500_006"));
                        this.BindData2PickDetailList(this.GetPickDetailQueryEntity());
                    }
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7500_007");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7500_004") + ex.Message;
                SysLog.GetInstance().LogError(ex);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type='text/javascript'> $.unblockUI();</script>", false);
            }
        }

        private PickDetailQueryEntity GetPickDetailQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as PickDetailQueryEntity);
            }
            return (base.PageQueryEntitySession as PickDetailQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                PickDetailQueryEntity pickDetailQuery = this.GetPickDetailQueryEntity();
                pickDetailQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2PickDetailList(pickDetailQuery);
            }
        }

        protected void gvPickDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7052", this.gvPickDetail);
        }

        protected void gvPickDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", string.Concat(new object[] { "GridViewCheckBoxClick('", e.Row.ClientID, "');SetRowIndex(", e.Row.RowIndex, ")" }));
                if (((TextBox) e.Row.FindControl("txtToLoc")) != null)
                {
                    switch (this.gvPickDetail.DataKeys[e.Row.RowIndex]["STATUS"].ToString().ToUpper())
                    {
                        case "NEW":
                        case "RELEASED":
                            ((TextBox) e.Row.FindControl("txtToLoc")).Enabled = true;
                            break;
                    }
                }
                if (((Label) e.Row.FindControl("lblQty")) != null)
                {
                    this.totalqty += decimal.Parse(string.IsNullOrEmpty(((Label) e.Row.FindControl("lblQty")).Text) ? "0" : ((Label) e.Row.FindControl("lblQty")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalQytText.Text = base.GetPreferenceDigit(this.totalqty);
            }
        }

        protected void gvPickDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                PickDetailQueryEntity pickDetailQuery = this.GetPickDetailQueryEntity();
                pickDetailQuery.SortField = e.SortExpression;
                if (pickDetailQuery.SortDirection == "ASC")
                {
                    pickDetailQuery.SortDirection = "DESC";
                }
                else if (pickDetailQuery.SortDirection == "DESC")
                {
                    pickDetailQuery.SortDirection = "ASC";
                }
                this.BindData2PickDetailList(pickDetailQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnCancelShipping_Click(object sender, EventArgs e)
        {
            List<string> strStatus = base.GetSelectedIDsInGridView(this.gvPickDetail, "STATUS");
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPickDetail, "PICK_DETAIL_ID");
            try
            {
                if (strStatus.Count > 0)
                {
                    int i;
                    for (i = 0; strStatus.Count > i; i++)
                    {
                        if (strStatus[i].ToString().ToUpper() != "SHIPPED")
                        {
                            this.lblMsg.Text = selectedID[i] + "该单号未发货无法取消!";
                            return;
                        }
                    }
                    if (((selectedID != null) && (selectedID.Count > 0)) && !string.IsNullOrEmpty(OutboundServiceClientProxy.GetInstance().CancelShipping(selectedID)))
                    {
                        string temp = string.Empty;
                        for (i = 0; i < selectedID.Count; i++)
                        {
                            temp = temp + selectedID[i] + ",";
                        }
                        this.lblMsg.Text = string.IsNullOrEmpty(temp) ? "" : (temp.Substring(0, temp.Length - 1) + "取消发货成功!");
                        this.BindData2PickDetailList(this.GetPickDetailQueryEntity());
                    }
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7500_003");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7500_004") + ex.Message;
                SysLog.GetInstance().LogError(ex);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type='text/javascript'> $.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnConfirmPicked_Click(object sender, EventArgs e)
        {
            this.ConfirmPicked();
        }

        protected void lnkbtnRevertPicked_Click(object sender, EventArgs e)
        {
            this.RevertPicked();
        }

        protected void lnkbtnRevertPickedAllocated_Click(object sender, EventArgs e)
        {
            this.RevertPickedAllocated();
        }

        protected void lnkbtnShipping_Click(object sender, EventArgs e)
        {
            this.Shipping();
        }

        private void LoadData()
        {
            PickDetailQueryEntity pickDetailQuery = new PickDetailQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    pickDetailQuery.OrderID = this.PreviousPage.OrderID;
                    pickDetailQuery.OwnerID = this.PreviousPage.OwnerID;
                    pickDetailQuery.SkuID = this.PreviousPage.SkuID;
                    pickDetailQuery.Status = this.PreviousPage.Status;
                    pickDetailQuery.ExternOrderID = this.PreviousPage.ExternOrderID;
                    pickDetailQuery.Lot = this.PreviousPage.Lot;
                    pickDetailQuery.FromLoc = this.PreviousPage.FromLoc;
                    pickDetailQuery.WaveID = this.PreviousPage.WaveID;
                    pickDetailQuery.ToLoc = this.PreviousPage.ToLoc;
                    pickDetailQuery.LpnID = this.PreviousPage.LpnID;
                    pickDetailQuery.ExternalLot = this.PreviousPage.ExternalLot;
                    pickDetailQuery.VendorID = this.PreviousPage.VendorID;
                    pickDetailQuery.ReferenceNum = this.PreviousPage.ReferenceNum;
                    pickDetailQuery.LocCategory = this.PreviousPage.LocCategory;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            pickDetailQuery.IsGetAll = false;
            pickDetailQuery.CurrentPage = "0";
            pickDetailQuery.PageSize = this.gvPickDetail.PageSize.ToString();
            pickDetailQuery.SortField = "PD.ORDER_ID";
            pickDetailQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvPickDetail.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetPickDetailQueryEntity(pickDetailQuery);
            this.BindData2PickDetailList(pickDetailQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void RevertPicked()
        {
            try
            {
                List<string> strStatus = base.GetSelectedIDsInGridView(this.gvPickDetail, "STATUS");
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPickDetail, "PICK_DETAIL_ID");
                if (strStatus.Count > 0)
                {
                    for (int i = 0; strStatus.Count > i; i++)
                    {
                        if (strStatus[i].ToString().ToUpper() != "PICKED")
                        {
                            this.lblMsg.Text = selectedID[i] + base.GetGlobalMsgResource("F7500_008");
                            return;
                        }
                    }
                    if ((selectedID != null) && (selectedID.Count > 0))
                    {
                        string result = OutboundServiceClientProxy.GetInstance().RevertPicked(selectedID, base.UserInfo.UserID);
                        if (!string.IsNullOrEmpty(result))
                        {
                            this.lblMsg.Text = result + base.GetGlobalMsgResource("F7500_009");
                            this.BindData2PickDetailList(this.GetPickDetailQueryEntity());
                        }
                    }
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7500_003");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7500_010") + ex.Message;
                SysLog.GetInstance().LogError(ex);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type='text/javascript'> $.unblockUI();</script>", false);
            }
        }

        private void RevertPickedAllocated()
        {
            try
            {
                List<string> strStatus = base.GetSelectedIDsInGridView(this.gvPickDetail, "STATUS");
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPickDetail, "PICK_DETAIL_ID");
                if (strStatus.Count > 0)
                {
                    for (int i = 0; strStatus.Count > i; i++)
                    {
                        if (strStatus[i].ToString().ToUpper() != "PICKED")
                        {
                            this.lblMsg.Text = selectedID[i] + base.GetGlobalMsgResource("F7500_008");
                            return;
                        }
                    }
                    if ((selectedID != null) && (selectedID.Count > 0))
                    {
                        string result = OutboundServiceClientProxy.GetInstance().RevertPickedAllocated(selectedID);
                        if (!string.IsNullOrEmpty(result))
                        {
                            this.lblMsg.Text = result + base.GetGlobalMsgResource("F7500_009");
                            this.BindData2PickDetailList(this.GetPickDetailQueryEntity());
                        }
                    }
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7500_003");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7500_010") + ex.Message;
                SysLog.GetInstance().LogError(ex);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type='text/javascript'> $.unblockUI();</script>", false);
            }
        }

        private void SetPickDetailQueryEntity(PickDetailQueryEntity pickDetailQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = pickDetailQuery;
            }
            else
            {
                base.PageQueryEntitySession = pickDetailQuery;
            }
        }

        private void Shipping()
        {
            try
            {
                List<string> strStatus = base.GetSelectedIDsInGridView(this.gvPickDetail, "STATUS");
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPickDetail, "PICK_DETAIL_ID");
                if (strStatus.Count > 0)
                {
                    int i;
                    string msg = "";
                    for (i = 0; strStatus.Count > i; i++)
                    {
                        if (strStatus[i].ToString().ToUpper() != "PICKED")
                        {
                            msg = msg + selectedID[i] + ";";
                        }
                    }
                    if (!string.IsNullOrEmpty(msg))
                    {
                        this.lblMsg.Text = msg + base.GetGlobalMsgResource("F7500_011");
                    }
                    else if (((selectedID != null) && (selectedID.Count > 0)) && !string.IsNullOrEmpty(OutboundServiceClientProxy.GetInstance().ConfirmShipped(selectedID, base.UserInfo.UserID)))
                    {
                        string temp = string.Empty;
                        for (i = 0; i < selectedID.Count; i++)
                        {
                            temp = temp + selectedID[i] + ",";
                        }
                        this.lblMsg.Text = string.IsNullOrEmpty(temp) ? "" : (temp.Substring(0, temp.Length - 1) + base.GetGlobalMsgResource("F7500_002"));
                        this.BindData2PickDetailList(this.GetPickDetailQueryEntity());
                    }
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7500_003");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7500_004") + ex.Message;
                SysLog.GetInstance().LogError(ex);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type='text/javascript'> $.unblockUI();</script>", false);
            }
        }

        public PickDetailQuery PreviousPage
        {
            get
            {
                return (PickDetailQuery) base.PreviousPage;
            }
        }
    }
}

